package top.drinyeit.nongmao.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.util.List;

import top.drinyeit.nongmao.R;
import top.drinyeit.nongmao.activity.CommodityDetailNewActivity;
import top.drinyeit.nongmao.activity.StoreActivity;
import top.drinyeit.nongmao.activity.WebViewActivity;
import top.drinyeit.nongmao.model.AppHomeBean;
import top.drinyeit.nongmao.view.CarouselView;

/**
 * Created by wu on 2018/1/8.
 */

public class CVAdapter implements CarouselView.Adapter {
    private List<AppHomeBean.BannerBean> list;
    private Context context;

    public CVAdapter(Context context, List<AppHomeBean.BannerBean> list) {
        this.context = context;
        this.list = setList(list);
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public View getView(final int position) {
        View view = View.inflate(context, R.layout.item, null);
        ImageView imageView = view.findViewById(R.id.image);
        Glide.with(context)
                .load(list.get(position).getBanner_img_url())
                .dontTransform()
                .placeholder(R.drawable.ic_def)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .into(imageView);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (list.get(position).getJump_type()) {
                    case 0:
                        Intent intent = new Intent(context, WebViewActivity.class);
                        intent.putExtra("url",list.get(position).getType_url());
                        context.startActivity(intent);
                        break;
                    case 1:
                        Intent intent1 = new Intent(context, CommodityDetailNewActivity.class);
                        intent1.putExtra("id", list.get(position).getType_id());
                        context.startActivity(intent1);
                        break;
                    default:
                        Intent intent2 = new Intent(context, StoreActivity.class);
                        intent2.putExtra("store_id", list.get(position).getType_id());
                        context.startActivity(intent2);
                        break;
                }


            }
        });
        return view;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public void clear() {
        this.list.clear();
    }

    private List<AppHomeBean.BannerBean> setList(List<AppHomeBean.BannerBean> list) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getBanner_img_url() == null || list.get(i).getJump_type() < 0) {
                list.remove(i);
            }
        }
        return list;
    }

}
